package com.stock.demo.util;

import android.text.TextUtils;

import java.util.Calendar;

/**
 * Created by xuzhaoyu on 17-7-22.
 */

public class StockUtils {

    //查询股票图片的4种类型
    public static final int TYPE_DIVIDE_TIME = 101;
    public static final int TYPE_DAY_K = 102;
    public static final int TYPE_WEEK_K = 103;
    public static final int TYPE_MONTH_K = 104;

    //判断当前时间是否在工作日且交易时间内
    public static boolean checkTradeTime() {
        Calendar calendar = Calendar.getInstance();
        //1，2，3，4，5，6，7
        //星期天，星期一，星期二，星期三，星期四，星期五，星期六
        int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
        if (weekDay == 1 || weekDay == 7) {
            return false;
        }
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        if (hour < 9 || hour > 14 || hour == 12 || (hour == 9 && minute <= 30) || (hour == 11 && minute >= 30)) {
            return false;
        }
        return true;
    }

    //根据代码查询股票信息的地址
    public static String getStockInfoUrl(String symbol) {
        if (TextUtils.isEmpty(symbol) || symbol.length() != 6) {
            return null;
        }
        if (TextUtils.equals("000001", symbol)) {
            return Urls.BASE_STOCK_URL_SH_INDEX + "000001";
        } else if (TextUtils.equals("399001", symbol)) {
            return Urls.BASE_STOCK_URL_SZ_INDEX + "399001";
        } else if (TextUtils.equals("399006", symbol)) {
            return Urls.BASE_STOCK_URL_SZ_INDEX + "399006";
        } else if ((!TextUtils.isEmpty(symbol)) && symbol.startsWith("6")) {
            return Urls.BASE_STOCK_URL_SH + symbol;
        } else if (!TextUtils.isEmpty(symbol)) {
            return Urls.BASE_STOCK_URL_SZ + symbol;
        } else {
            return null;
        }
    }

    //根据代码查询股票图片的地址
    public static String getStockImageUrl(String symbol, int type) {
        if (TextUtils.isEmpty(symbol) || symbol.length() != 6) {
            return null;
        }
        if (type != TYPE_DIVIDE_TIME && type != TYPE_DAY_K && type != TYPE_WEEK_K && type != TYPE_MONTH_K) {
            return null;
        }
        switch (type) {
            case TYPE_DIVIDE_TIME:
                if (TextUtils.equals("000001", symbol)) {
                    return Urls.BASE_STOCK_IMAGE_DIVIDE_TIME_URL_SH + "000001.gif";
                } else if (TextUtils.equals("399001", symbol)) {
                    return Urls.BASE_STOCK_IMAGE_DIVIDE_TIME_URL_SZ + "399001.gif";
                } else if (TextUtils.equals("399006", symbol)) {
                    return Urls.BASE_STOCK_IMAGE_DIVIDE_TIME_URL_SZ + "399006.gif";
                } else if ((!TextUtils.isEmpty(symbol)) && symbol.startsWith("6")) {
                    return Urls.BASE_STOCK_IMAGE_DIVIDE_TIME_URL_SH + symbol + ".gif";
                } else if (!TextUtils.isEmpty(symbol)) {
                    return Urls.BASE_STOCK_IMAGE_DIVIDE_TIME_URL_SZ + symbol + ".gif";
                } else {
                    return null;
                }
            case TYPE_DAY_K:
                if (TextUtils.equals("000001", symbol)) {
                    return Urls.BASE_STOCK_IMAGE_DAY_K_URL_SH + "000001.gif";
                } else if (TextUtils.equals("399001", symbol)) {
                    return Urls.BASE_STOCK_IMAGE_DAY_K_URL_SZ + "399001.gif";
                } else if (TextUtils.equals("399006", symbol)) {
                    return Urls.BASE_STOCK_IMAGE_DAY_K_URL_SZ + "399006.gif";
                } else if ((!TextUtils.isEmpty(symbol)) && symbol.startsWith("6")) {
                    return Urls.BASE_STOCK_IMAGE_DAY_K_URL_SH + symbol + ".gif";
                } else if (!TextUtils.isEmpty(symbol)) {
                    return Urls.BASE_STOCK_IMAGE_DAY_K_URL_SZ + symbol + ".gif";
                } else {
                    return null;
                }
            case TYPE_WEEK_K:
                if (TextUtils.equals("000001", symbol)) {
                    return Urls.BASE_STOCK_IMAGE_WEEK_K_URL_SH + "000001.gif";
                } else if (TextUtils.equals("399001", symbol)) {
                    return Urls.BASE_STOCK_IMAGE_WEEK_K_URL_SZ + "399001.gif";
                } else if (TextUtils.equals("399006", symbol)) {
                    return Urls.BASE_STOCK_IMAGE_WEEK_K_URL_SZ + "399006.gif";
                } else if ((!TextUtils.isEmpty(symbol)) && symbol.startsWith("6")) {
                    return Urls.BASE_STOCK_IMAGE_WEEK_K_URL_SH + symbol + ".gif";
                } else if (!TextUtils.isEmpty(symbol)) {
                    return Urls.BASE_STOCK_IMAGE_WEEK_K_URL_SZ + symbol + ".gif";
                } else {
                    return null;
                }
            case TYPE_MONTH_K:
                if (TextUtils.equals("000001", symbol)) {
                    return Urls.BASE_STOCK_IMAGE_MONTH_K_URL_SH + "000001.gif";
                } else if (TextUtils.equals("399001", symbol)) {
                    return Urls.BASE_STOCK_IMAGE_MONTH_K_URL_SZ + "399001.gif";
                } else if (TextUtils.equals("399006", symbol)) {
                    return Urls.BASE_STOCK_IMAGE_MONTH_K_URL_SZ + "399006.gif";
                } else if ((!TextUtils.isEmpty(symbol)) && symbol.startsWith("6")) {
                    return Urls.BASE_STOCK_IMAGE_MONTH_K_URL_SH + symbol + ".gif";
                } else if (!TextUtils.isEmpty(symbol)) {
                    return Urls.BASE_STOCK_IMAGE_MONTH_K_URL_SZ + symbol + ".gif";
                } else {
                    return null;
                }
            default:
                return null;
        }

    }

}
